package com.ruoyi.basedata.controller;

import com.ruoyi.basedata.service.IGoodsService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.basedata.Goods;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 商品Controller
 *
 * @author wl
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/goods")
@Slf4j
public class GoodsController extends BaseController {
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询商品列表
     */
    @RequiresPermissions("basedata:goods:list")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods) {
        log.info("GoodsController list goods:{}", goods);
        List<Goods> goodsList = goodsService.selectGoodsList(goods);
        TableDataInfo responseResult = getDataTable(goodsList);
        log.info("GoodsController list responseResult:{} goods:{}", responseResult, goods);
        return responseResult;
    }

    /**
     * 获取所有商品列表
     */
    //    @RequiresPermissions("basedata:goods:list:all")
    @GetMapping("/list/all")
    public AjaxResult listAll() {
        log.info("GoodsController listAll start ...");
        AjaxResult ajaxResult = goodsService.listAll();
        log.info("GoodsController listAll end ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    @InnerAuth
    @PostMapping("/getListByIds")
    public R<List<Goods>> getGoodsListByIds(@RequestBody List<Integer> ids) {
        log.info("GoodsController getGoodsListByIds ids params {}", ids);
//        goodsService.selectGoodsListByIds(ids);
        return R.ok(goodsService.selectGoodsListByIds(ids));
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据商品id获取成本
     * @date 2024/11/8 上午10:01
     * @Param goodsId:
     **/
    @RequiresPermissions("basedata:goods:query")
    @GetMapping("/cost/goodsId/{id}")
    public R<BigDecimal> getCostByGoodsId(@PathVariable("id") Integer id) {
        log.info("GoodsController getCostByGoodsId id:{}", id);
        R<BigDecimal> costR = goodsService.getCostByGoodsId(id);
        log.info("GoodsController getCostByGoodsId costR:{}", costR);
        return costR;
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("basedata:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods) {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @RequiresPermissions("basedata:goods:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(goodsService.selectGoodsById(id));
    }

    @PostMapping("/getGoodsById")
    public R<Goods> getGoodsById(@RequestBody Integer id) {
        log.info("getGoodsById params:{}", id);
        return R.ok(goodsService.selectGoodsById(id));
    }

    /**
     * 新增商品
     */
    @RequiresPermissions("basedata:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods) {
        log.info("GoodsController add goods:{}", goods);
        AjaxResult ajaxResult = goodsService.insertGoods(goods);
        log.info("GoodsController add ajaxResult:{} goods:{}", ajaxResult, goods);
        return ajaxResult;
    }

    /**
     * 校验商品编码是否重复
     */
//    @RequiresPermissions("basedata:goods:checkProCode")
    @GetMapping("/checkProCode")
    public AjaxResult checkProCode(String proCode) {
        log.info("GoodsController checkProCode proCode:{}", proCode);
        AjaxResult ajaxResult = goodsService.checkProCode(proCode);
        log.info("GoodsController checkProCode ajaxResult:{} proCode:{}", ajaxResult, proCode);
        return ajaxResult;
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("basedata:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods) {
        log.info("GoodsController edit goods:{}", goods);
        AjaxResult ajaxResult = goodsService.updateGoods(goods);
        log.info("GoodsController edit ajaxResult:{} goods:{}", ajaxResult, goods);
        return ajaxResult;
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("basedata:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        log.info("GoodsController remove ids:{}", Arrays.toString(ids));
        AjaxResult ajaxResult = goodsService.deleteGoodsByIds(ids);
        log.info("GoodsController remove ajaxResult:{} ids:{}", ajaxResult, ids);
        return ajaxResult;
    }

}
